package com.das.mechanic_base.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3CarPaintView extends View {

    private Context mContext;

    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 圆点画笔
     */
    private Paint cPaint;

    /**
     * 文字画笔
     */
    private Paint tPaint;

    private int startAngel = 90;

    /**
     * 开始的动画角度
     */
    private int startAnimAngel = 90;

    /**
     * 半径
     */
    private int radius;

    private int centerX,centerY;

    /**
     * 圆弧上的圆点
     */
    private float middleX,middleY,rightY,rightX,leftX,leftY,frontX,frontY,backX,backY,bitmapX,bitmapY;

    private Bitmap noticeBitmap;

    private String car_back,car_left,car_middle,car_right,car_front;

    private int clickWidth;


    public X3CarPaintView(Context context) {
        this(context,null);
    }

    public X3CarPaintView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3CarPaintView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setDither(true);
        mPaint.setStrokeWidth(X3ScreenUtils.dipToPx(1,mContext));

        cPaint = new Paint();
        cPaint.setStyle(Paint.Style.FILL);
        cPaint.setAntiAlias(true);
        cPaint.setColor(Color.parseColor("#0077ff"));
        cPaint.setDither(true);

        tPaint = new Paint();
        tPaint.setStyle(Paint.Style.FILL);
        tPaint.setAntiAlias(true);
        tPaint.setTextSize(X3ScreenUtils.dip2px(12,mContext));
        tPaint.setColor(Color.parseColor("#B1B3BD"));
        tPaint.setDither(true);

        noticeBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_car_alone_select_icon, null);

        car_back = mContext.getString(R.string.x3_car_back);
        car_left = mContext.getString(R.string.x3_car_left);
        car_middle = mContext.getString(R.string.x3_car_middle);
        car_right = mContext.getString(R.string.x3_car_right);
        car_front = mContext.getString(R.string.x3_car_front);

        clickWidth = X3ScreenUtils.dipToPx(30,mContext);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        centerX = getWidth() / 2;
        centerY = - ((getWidth() + X3ScreenUtils.dipToPx(300,mContext)) / 2 - getHeight() / 2);
        radius = (getWidth() + X3ScreenUtils.dipToPx(300,mContext)) / 2;

        SweepGradient mSweepGradient = new SweepGradient(centerX,centerY,
                new int[]{Color.parseColor("#000077ff"),Color.parseColor("#000077ff"),Color.parseColor("#000077ff"),
                        Color.parseColor("#000077ff"),Color.parseColor("#000077ff"),Color.parseColor("#0077ff"),
                        Color.parseColor("#000077ff"),Color.parseColor("#000077ff"),Color.parseColor("#000077ff"),
                        Color.parseColor("#000077ff"),Color.parseColor("#000077ff")},null);

        Matrix matrix = new Matrix();
        matrix.setRotate(-90f,centerX,centerY);
        mSweepGradient.setLocalMatrix(matrix);
        mPaint.setShader(mSweepGradient);

        canvas.drawCircle(centerX,centerY,radius,mPaint);

        cPaint.setColor(Color.parseColor("#0077ff"));
        middleX = (float) (centerX + radius * Math.cos(startAngel * Math.PI / 180));
        middleY = (float) (centerY + radius * Math.sin(startAngel * Math.PI / 180));
        canvas.drawCircle(middleX, middleY, X3ScreenUtils.dipToPx(4,mContext),cPaint);

        cPaint.setColor(Color.parseColor("#800077ff"));
        rightX = (float) (centerX + radius * Math.cos((startAngel - 12) * Math.PI / 180));
        rightY = (float) (centerY + radius * Math.sin((startAngel - 12) * Math.PI / 180));
        canvas.drawCircle(rightX, rightY, X3ScreenUtils.dipToPx(3,mContext),cPaint);

        leftX = (float) (centerX + radius * Math.cos((startAngel + 12) * Math.PI / 180));
        leftY = (float) (centerY + radius * Math.sin((startAngel + 12) * Math.PI / 180));
        canvas.drawCircle(leftX, leftY, X3ScreenUtils.dipToPx(3,mContext),cPaint);

        cPaint.setColor(Color.parseColor("#600077ff"));
        frontX = (float) (centerX + radius * Math.cos((startAngel - 24) * Math.PI / 180));
        frontY = (float) (centerY + radius * Math.sin((startAngel - 24) * Math.PI / 180));
        canvas.drawCircle(frontX, frontY, X3ScreenUtils.dipToPx(2,mContext),cPaint);

        backX = (float) (centerX + radius * Math.cos((startAngel + 24) * Math.PI / 180));
        backY = (float) (centerY + radius * Math.sin((startAngel + 24) * Math.PI / 180));
        canvas.drawCircle(backX, backY, X3ScreenUtils.dipToPx(2,mContext),cPaint);

        if (bitmapY == 0 && bitmapX == 0) {
            bitmapY = backY;
            bitmapX = backX;
        }
        canvas.drawBitmap(noticeBitmap,bitmapX - noticeBitmap.getWidth() / 2, bitmapY - noticeBitmap.getWidth() / 2,mPaint);

        Rect rect = new Rect();
        tPaint.getTextBounds(car_back,0,car_back.length(),rect);
        canvas.drawText(car_back,middleX - rect.width() / 2,middleY + X3ScreenUtils.dipToPx(36,mContext),tPaint);

        tPaint.getTextBounds(car_front,0,car_front.length(),rect);
        canvas.drawText(car_front,backX - rect.width() / 2,backY + X3ScreenUtils.dipToPx(36,mContext),tPaint);

        tPaint.getTextBounds(car_left,0,car_left.length(),rect);
        canvas.drawText(car_left,leftX - rect.width() / 2,leftY + X3ScreenUtils.dipToPx(36,mContext),tPaint);

        tPaint.getTextBounds(car_right,0,car_right.length(),rect);
        canvas.drawText(car_right,rightX - rect.width() / 2,rightY + X3ScreenUtils.dipToPx(36,mContext),tPaint);

        tPaint.getTextBounds(car_middle,0,car_middle.length(),rect);
        canvas.drawText(car_middle,frontX - rect.width() / 2,frontY + X3ScreenUtils.dipToPx(36,mContext),tPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            float x = event.getX();
            float y = event.getY();

            if (x <= middleX + clickWidth && x >= middleX - clickWidth && y <= middleY + clickWidth && y >= middleY - clickWidth) {
                if (iOnClickChangeCar != null) {
                    iOnClickChangeCar.iOnClickChange(0);
                }

                if (bitmapX == middleX && bitmapY == middleY ) {
                    return false;
                }
                startAnim(90,0);

            }else if (x <= backX + clickWidth && x >= backX - clickWidth && y <= backY + clickWidth && y >= backY - clickWidth) {
                if (iOnClickChangeCar != null) {
                    iOnClickChangeCar.iOnClickChange(1);
                }
                if (bitmapX == backX && bitmapY == backY) {
                    return false;
                }
                startAnim(114,1);

            }else if (x <= leftX + clickWidth && x >= leftX - clickWidth && y <= leftY + clickWidth && y >= leftY - clickWidth) {
                if (iOnClickChangeCar != null) {
                    iOnClickChangeCar.iOnClickChange(2);
                }
                if (bitmapX == leftX && bitmapY == leftY) {
                    return false;
                }
                startAnim(102,2);
            }else if (x <= rightX + clickWidth && x >= rightX - clickWidth && y <= rightY + clickWidth && y >= rightY - clickWidth) {
                if (iOnClickChangeCar != null) {
                    iOnClickChangeCar.iOnClickChange(3);
                }
                if (bitmapX == rightX && bitmapY == rightY ) {
                    return false;
                }
                startAnim(78,3);

            }else if (x <= frontX + clickWidth && x >= frontX - clickWidth && y <= frontY + clickWidth && y >= frontY - clickWidth) {
                if (iOnClickChangeCar != null) {
                    iOnClickChangeCar.iOnClickChange(4);
                }
                if (bitmapX == frontX && bitmapY == frontY ) {
                    return false;
                }
                startAnim(66,4);
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 图片的动画
     * @param endAngel
     */
    public void startAnim(int endAngel,int type) {

        int time = 0;
        int abs = Math.abs(endAngel - startAnimAngel);
        if (abs <= 12) {
            time = 150;
        }else if (abs <= 24) {
            time = 180;
        } else{
            time = 300;
        }

        ValueAnimator valueY = ValueAnimator.ofInt(startAnimAngel,endAngel);
        valueY.setDuration(time);
        valueY.addUpdateListener(animation -> {
            int angel = (int) animation.getAnimatedValue();

            bitmapX = (float) (centerX + radius * Math.cos(angel * Math.PI / 180));
            bitmapY = (float) (centerY + radius * Math.sin(angel * Math.PI / 180));
            invalidate();
        });
        valueY.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                switch (type) {
                    case 0:
                        startAnimAngel = 90;
                        break;
                    case 1:
                        startAnimAngel = 114;
                        break;
                    case 2:
                        startAnimAngel = 102;
                        break;
                    case 3:
                        startAnimAngel = 78;
                        break;
                    case 4:
                        startAnimAngel = 66;
                        break;
                }
            }
        });

        valueY.start();
    }

    IOnClickChangeCar iOnClickChangeCar;

    public void setiOnClickChangeCar(IOnClickChangeCar iOnClickChangeCar) {
        this.iOnClickChangeCar = iOnClickChangeCar;
    }

    public interface IOnClickChangeCar {
        void iOnClickChange(int type);
    }
}
